<?php
namespace app\api\controller;
use think\facade\Db;
class Type extends Base{
    //后端 分类列表
    public function list_data_a(){
        $param = input("");
        $listData = Db::table('an_type')
        ->where('type', $param['type'])
        ->order('sort', 'desc')
        ->page($param['page'],$param['limit'])
        ->select()
        ->toArray(); 
        $this->info['data'] = $listData;
        $this->info['total'] = Db::table('an_type')
        ->where('type', $param['type'])
        ->count();
        return json($this->info);
    }

    //前端 分类列表
    public function type_list_data(){
        $param = input("");
        $page = isset($param['page'])?$param['page']:1;
        $limit = isset($param['limit'])?$param['limit']:100;
        $listData = Db::table('an_type')
        ->where('type', $param['type'])
        ->where('a_show', '1')
        ->order('sort', 'desc')
        ->page($page,$limit)
        ->select()
        ->toArray(); 
        $this->info['data'] = $listData;
        $this->info['total'] = Db::table('an_type')
        ->where('type', $param['type'])
        ->where('a_show', '1')
        ->count();
        return json($this->info);
    }

    //分类添加、编辑
    public function editType(){
        $param = input("");
        $data = [
            "up_time"=>time(),
            "name"=>trim($param['name']),
            "pid"=>trim($param['pid']),
            "sort"=>trim($param['sort']),
            "type"=>trim($param['type']),
            "img"=>isset($param['img'])?$param['img']:'',
            "a_show"=>trim($param['a_show']),
            "other"=>trim($param['other']),
        ];
        if(empty($param['id'])){
            $data['create_time'] = time();
            $this->info['msg']='新增分类';
        }else{
            $data['id'] = $param['id'];
            $this->info['msg']='编辑分类';
        }
        $this->logData($this->info['msg'].'，名称为'.$param['name']);

        //save 自动判断是编辑更新数据 还是 插入新数据
        $this->info['data'] = Db::table('an_type')->save($data);
        return json($this->info);
    }

    //删除分类
    public function type_del_data(){
        $param = input("");
        $bool = Db::table('an_type')->where('id',$param['id'])->delete();
        $this->logData('删除分类'.$param['name']);
        $this->info = ['code'=>200,'msg'=>'成功'];
        return json($this->info);
    }
}
